import {login, logout, getInfo, getAlioss} from '@/api/login'
import {getToken, setToken, removeToken, setExpiresIn, getShopList} from '@/utils/auth'
import {queryUserShopList} from "@/api/shop";
import {debug} from "script-ext-html-webpack-plugin/lib/common";

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    region: '',
    accessKeyId: '',
    accessKeySecret: '',
    bucket: '',
    prefix: '',
    domain: '',
    userId: '',
    shopList: getShopList()
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_EXPIRES_IN: (state, time) => {
      state.expires_in = time
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USERINFO: (state, urserInfo) => {
      state.urserInfo = urserInfo
    },
    SET_REGION: (state, region) => {
      state.region = region
    },
    SET_ACCESSKEYID: (state, accessKeyId) => {
      state.accessKeyId = accessKeyId
    },
    SET_ACCESSKEYSECRET: (state, accessKeySecret) => {
      state.accessKeySecret = accessKeySecret
    },
    SET_BUCKET: (state, bucket) => {
      state.bucket = bucket
    },
    SET_PREFIX: (state, prefix) => {
      state.prefix = prefix
    },
    SET_DOMAIN: (state, domain) => {
      state.domain = domain
    },
    SET_SHOP_LIST: (state, shopList) => {
      state.shopList = shopList
    }
  },

  actions: {
    // 登录
    Login({commit}, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid).then(res => {
          let data = res.data
          setToken(data.access_token)
          commit('SET_TOKEN', data.access_token)
          setExpiresIn(data.expires_in)
          commit('SET_EXPIRES_IN', data.expires_in)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        // 用户信息
        getInfo().then(res => {
          const user = res.user
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
          commit('SET_USERID', user.userId)
          commit('SET_AVATAR', avatar)
          commit("SET_USERINFO", user)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取阿里oss资料
    getAliossInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        // 资料签名
        getAlioss().then(res => {
          const isHttps = res.data.isHttps
          commit('SET_DOMAIN', res.data.domain + '/')
          commit('SET_REGION', res.data.region)
          commit('SET_ACCESSKEYID', res.data.accessKey)
          commit('SET_ACCESSKEYSECRET', res.data.secretKey)
          commit('SET_BUCKET', res.data.bucketName)
          commit('SET_PREFIX', res.data.prefix)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取店铺列表
    GetShopList({commit, state}) {
      return new Promise((resolve, reject) => {
        queryUserShopList().then((res) => {
          let list = JSON.stringify(res.rows)
          // let list = res.rows
          commit('SET_SHOP_LIST', list)
          // setShopList(list)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 退出系统
    LogOut({commit, state}) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
